/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package atm;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;

/**
 *
 * @author henrikj
 */
public class StartScreen implements Initializable {
    
    private int id;
    private int balance;
    
    
    public Pane ContentPane;
    DbHandler db = new DbHandler();
    
    @FXML
    private TextField password;

    @FXML
    private TextField withdrawsum;
    
    @FXML
    private Text balancetext;
    
    @FXML
    private Pane balancedialog;
    
    @FXML
    private Pane login;

    public void insertCardButtonAction(ActionEvent event) throws IOException {
        ContentPane.getChildren().clear();
        ContentPane.getChildren().add((Node) FXMLLoader.load(getClass().getResource("login.fxml")));
        db.connect();
    }

    @FXML
    public void loginButtonAction(ActionEvent event) throws IOException {
        ContentPane = (Pane) login.getParent();
        String pass;
        pass = password.getText();
        int userid;
        try {
            userid = db.logIn(Integer.parseInt(pass));
        } catch (Exception e) {
            userid=0;
            System.out.println("Wrong password!");
        }
        if(userid>0) {
            Context.getInstance().currentId().setUserId(userid);
            ContentPane.getChildren().clear();
            ContentPane.getChildren().add((Node) FXMLLoader.load(getClass().getResource("balanceorwithdraw.fxml")));
        }
    }

    @FXML
    public void chooseWithdrawButtonAction(ActionEvent event) throws IOException {
        ContentPane = (Pane) login.getParent();
        ContentPane.getChildren().clear();
        ContentPane.getChildren().add((Node) FXMLLoader.load(getClass().getResource("withdraw.fxml")));
    }

    @FXML
    public void chooseBalanceButtonAction(ActionEvent event) throws IOException {
        ContentPane = (Pane) login.getParent();
        ContentPane.getChildren().clear();
        ContentPane.getChildren().add((Node) FXMLLoader.load(getClass().getResource("welcomebackwithbalance.fxml")));
    }

    @FXML
    public void withdrawButtonAction(ActionEvent event) throws IOException {
        String sum = withdrawsum.getText();
        balance = db.checkBalance(id);
        if(balance>Integer.parseInt(sum)) {
            db.withdraw(id, Integer.parseInt(sum));
            ContentPane = (Pane) login.getParent();
            ContentPane.getChildren().clear();
            ContentPane.getChildren().add((Node) FXMLLoader.load(getClass().getResource("showbalancedialog.fxml")));
        } else {
            System.out.println("SKAFFA MER PENGAR!");
        }
    }
    
    @FXML
    public void quicksumTwoWithdrawButtonAction(ActionEvent event) throws IOException {
        db.withdraw(id, 200);
        ContentPane = (Pane) login.getParent();
        ContentPane.getChildren().clear();
        ContentPane.getChildren().add((Node) FXMLLoader.load(getClass().getResource("showbalancedialog.fxml")));
    }
    @FXML
    public void quicksumFiveWithdrawButtonAction(ActionEvent event) throws IOException {
        db.withdraw(id, 500);
        ContentPane = (Pane) login.getParent();
        ContentPane.getChildren().clear();
        ContentPane.getChildren().add((Node) FXMLLoader.load(getClass().getResource("showbalancedialog.fxml")));
    }
    
    @FXML
    public void quicksumTenWithdrawButtonAction(ActionEvent event) throws IOException {
        db.withdraw(id, 1000);
        ContentPane = (Pane) login.getParent();
        ContentPane.getChildren().clear();
        ContentPane.getChildren().add((Node) FXMLLoader.load(getClass().getResource("showbalancedialog.fxml")));
    }
    
    @FXML
    public void cancelButtonAction(ActionEvent event) throws IOException {
        //ContentPane = (Pane) login.getParent();
        ContentPane.getChildren().clear();
        ContentPane.getChildren().add((Node) FXMLLoader.load(getClass().getResource("Start.fxml")));
        Context.getInstance().reset();
        System.out.println("Testing: " + id);
        db.disconnect();
    }
    
    public void chooseNoBalanceInDialogButtonAction(ActionEvent event) throws IOException {
        ContentPane = (Pane) balancedialog.getParent();
        ContentPane.getChildren().clear();
        ContentPane.getChildren().add((Node) FXMLLoader.load(getClass().getResource("welcomeback.fxml")));   
    }
    
    public void chooseBalanceInDialogButtonAction(ActionEvent event) throws IOException {
        ContentPane = (Pane) balancedialog.getParent();
        ContentPane.getChildren().clear();
        ContentPane.getChildren().add((Node) FXMLLoader.load(getClass().getResource("welcomebackwithbalance.fxml")));
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        id=Context.getInstance().currentId().getUserId();
    } 
}
